home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: netcom.com!marnold
- From: marnold@netcom.com (Matt Arnold)
- Subject: Re: The STL and nested structures
- Message-ID: <marnoldDpHB2u.7G2@netcom.com>
- Organization: NETCOM On-line Communication Services (408 261-4700 guest)
- References: <4ju9q7$fa1$1@mhadg.production.compuserve.com>
- Date: Sun, 7 Apr 1996 06:23:18 GMT
- Sender: marnold@netcom19.netcom.com
-
- Alan Huff <74312.2300@CompuServe.COM> writes:
-
- >I am having a problem using the STL with structures defined within
- >a class definition. Consider the following code fragment.
-
- >>#include <vector.h>
- >>class Bar {
- >> struct Foo {
- >> int value;
- >> };
- >>
- >> vector< Foo > fooContainer;
- >>}
-
- >The compiler I am using (VC 4.1) refuses to compile. An error is
- >generated the says "'Foo' : undeclared identifier" at line 75 in
- >vector.h.
-
- >Line 75 of vector.h
- >> vector(size_type n, const T& value = T()) { ...
-
- >If I move the structure Foo out of the class and into the global
- >namespace there are no compilation errors.
-
- >What am I missing??
-
- A real C++ compiler.
-
- It looks like Visual C++ 4.1 is trying to instantiate vector for
- Bar::fooContainer with a type (Bar::Foo) that it hasn't recognized
- yet. Since the definition of Foo appears before the declaration of
- fooContainer in Bar, this shouldn't be happening. The above code
- should compile without problems (it certainly does with Borland C++).
- Why the latest version of Visual C++ has a different "opinion" about
- this, I can't answer.
-
- You can try the following to see if it helps...
-
- class Bar {
- struct Foo {
- int value;
- };
- vector< Bar::Foo > fooContainer;
- };
-
- ...but explicity qualifying a nested class like this should not be
- required for statements *within* the nesting class' scope.
-
- You might even try a forward declaration of Foo to see if that "jogs"
- Visual C++ into realizing Bar::Foo exists at the right time, although
- such a forward reference is obviously redundant...
-
- class Bar {
- struct Foo {
- int value;
- };
- struct Foo;
- vector< Foo > fooContainer;
- };
-
- As a test, you can also replace vector with your own simple template
- (that does something similar to vector with the parameterized type)
- and see if you get the same kind of error...
-
- template <class T>
- struct Test {
- Test(const T& = T()) { }
- };
-
- class Bar {
- struct Foo {
- int value;
- };
- Test< Foo > fooTest;
- };
-
- If so, you'll know the problem has nothing to do with STL, but rather
- the general template implementation of your compiler.
-
- If none of this leads you to any kind of solution, I guess you'll be
- forced to avoid using nested classes this way with Dismal, uh, Visual
- C++, or you could switch to a compiler which can deal with the above
- code. If you like, you could also complain to Microsoft.
-
- Regards,
- -------------------------------------------------------------------------
- Matt Arnold | | ||| | |||| | | | || ||
- marnold@netcom.com | | ||| | |||| | | | || ||
- Boston, MA | 0 | ||| | |||| | | | || ||
- 617.389.7384 (h) 617.576.2760 (w) | | ||| | |||| | | | || ||
- C++, MIDI, Win32/95 developer | | ||| 4 3 1 0 8 3 || ||
- -------------------------------------------------------------------------
-